﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using FlexiGridHelper;
using System.Data;

namespace Golf.WebUI.HttpHandler.Commodities
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary> 
    public class Golf_ProductList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            JavaScriptSerializer js = new JavaScriptSerializer();
            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            List<Golf.Model.Golf_Product> list = new List<Golf.Model.Golf_Product>();
            //实例化页面信息类，获取页面传来的参数。该类是FlexiGridHelper中定义的类，封装了很多功能需要的数据
            PageView pageView = new PageView(context);
            //获取分页后的数据集
            DataSet ds = bll.GetList(pageView.PageSize, pageView.PageIndex, "");
            //构建表格
            DataTable dt = new DataTable();
            //构建列
            //注意：第一列必须是数据库的主键列
            DataColumn dc0 = new DataColumn("Id", typeof(string));
            DataColumn dc1 = new DataColumn("Pro_TypeId", typeof(string));
            DataColumn dc2 = new DataColumn("Pro_BrandTypeId", typeof(string));
            DataColumn dc3 = new DataColumn("Pro_Name", typeof(string));
            DataColumn dc4 = new DataColumn("Pro_Number", typeof(string));
            DataColumn dc5 = new DataColumn("Pro_BuyNum", typeof(string));
            DataColumn dc6 = new DataColumn("Pro_Picture", typeof(string));
            DataColumn dc7 = new DataColumn("Pro_PicSmall", typeof(string));
            DataColumn dc8 = new DataColumn("Pro_PicBig", typeof(string));
            DataColumn dc9 = new DataColumn("Pro_SeoTitle", typeof(string));
            DataColumn dc10 = new DataColumn("Pro_Wood", typeof(string));
            DataColumn dc11 = new DataColumn("Pro_Save", typeof(string));
            DataColumn dc12 = new DataColumn("Pro_MarketPrice", typeof(string));
            DataColumn dc13 = new DataColumn("Pro_Price", typeof(string));
            DataColumn dc14 = new DataColumn("IsPromotion", typeof(string));
            DataColumn dc15 = new DataColumn("Pro_PromotionPrice", typeof(string));
            DataColumn dc16 = new DataColumn("PromotionStartTime", typeof(string));
            DataColumn dc17 = new DataColumn("PromotionEndTime", typeof(string));
            DataColumn dc18 = new DataColumn("Pro_Usage", typeof(string));
            DataColumn dc19 = new DataColumn("Pro_Introduction", typeof(string));
            DataColumn dc20 = new DataColumn("IsShow", typeof(string));
            DataColumn dc21 = new DataColumn("isCommend", typeof(string));
            DataColumn dc22 = new DataColumn("Pro_AddTime", typeof(string));
            DataColumn dc23 = new DataColumn("LimitBuyNum", typeof(string));
            DataColumn dc24 = new DataColumn("InventoryNum", typeof(string));
            DataColumn dc25 = new DataColumn("IsStock", typeof(string));
            DataColumn dc26 = new DataColumn("Pro_CommentNum", typeof(string));
            DataColumn dc27 = new DataColumn("AdminId", typeof(string));
            DataColumn dc28 = new DataColumn("IsGrounding", typeof(string));
            DataColumn dc29 = new DataColumn("Pro_Star", typeof(string));
            //把构建的列添加到数据表中
            dt.Columns.AddRange(new DataColumn[] { dc0, dc1, dc2, dc3, dc4, dc5, dc6, dc7, dc8, dc9, dc10, dc11, dc12, dc13, dc14, dc15, dc16, dc17, dc18, dc19, dc20, dc21, dc22, dc23, dc24, dc25, dc26, dc27, dc28, dc29 });//这里有个顺序的哟 
            list = bll.DataTableToList(ds.Tables[0]);//获取第一张表的数据  就是数据库中的数据
            //填充数据
            foreach (var item in list)
            {
                //新建行，注意是dt.NewRow();而不是new DataRow();
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                dr["Id"] = item.Id;
                dr["Pro_TypeId"] = item.Pro_TypeId;
                dr["Pro_BrandTypeId"] = item.Pro_BrandTypeId;
                dr["Pro_Name"] = item.Pro_Name;
                dr["Pro_Number"] = item.Pro_Number;
                dr["Pro_BuyNum"] = item.Pro_BuyNum;
                dr["Pro_Picture"] = item.Pro_Picture;
                dr["Pro_PicSmall"] = item.Pro_PicSmall;
                dr["Pro_PicBig"] = item.Pro_PicBig;
                dr["Pro_SeoTitle"] = item.Pro_SeoTitle;
                dr["Pro_Wood"] = item.Pro_Wood;
                dr["Pro_Save"] = item.Pro_Save;
                dr["Pro_MarketPrice"] = item.Pro_MarketPrice;
                dr["Pro_Price"] = item.Pro_Price;
                dr["IsPromotion"] = item.IsPromotion;
                dr["Pro_PromotionPrice"] = item.Pro_PromotionPrice;
                dr["PromotionStartTime"] = item.PromotionStartTime;
                dr["PromotionEndTime"] = item.PromotionEndTime;
                dr["Pro_Usage"] = item.Pro_Usage;
                dr["Pro_Introduction"] = item.Pro_Introduction;
                dr["IsShow"] = item.IsShow;
                dr["isCommend"] = item.isCommend;
                dr["Pro_AddTime"] = item.Pro_AddTime;
                dr["LimitBuyNum"] = item.LimitBuyNum;
                dr["InventoryNum"] = item.InventoryNum;
                dr["IsStock"] = item.IsStock;
                dr["Pro_CommentNum"] = item.Pro_CommentNum;
                dr["AdminId"] = item.AdminId;
                dr["IsGrounding"] = item.IsGrounding;
                dr["Pro_Star"] = item.Pro_Star;
            }
            //调用FlexiGrid数据格式转换方法
            context.Response.Write(js.Serialize(new GetDataHelper().GetFlexiGridData(pageView, dt, (int)ds.Tables[1].Rows[0][0])));//ds.Tables[1].Rows[0][0] 这里是数据库表的记录行数  是第二张表的 在dao类中的最后方法中

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
